home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir24 / jnos110g.zip / OLDBIDS.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  4KB  |  126 lines

  1. /* This program will deleted old BID's from the history file,
  2.  * after making a backup copy.
  3.  * If no arguments are given, anything older then 30 days will
  4.  * be deleted.
  5.  * By default, the history file is '/spool/history'
  6.  * on the current drive.
  7.  * Optional parameters:
  8.  *      -dpath, where 'path' is the path to the history file
  9.  *          (path can have an optional ending '/')
  10.  *      #, where # is the age of bids to expire
  11.  *
  12.  *  Eg. 'oldbids 30 -d/nos/spool/' will delete all bids older then
  13.  *      30 days from the file '/nos/spool/history'
  14.  *
  15.  * Copyright 1992, Johan. K. Reinalda, WG7J/PA3DIS
  16.  *      email : johan@ece.orst.edu
  17.  *      packet: wg7j@wg7j.or.usa.na
  18.  *
  19.  * Any part of this source may be freely distributed for none-commercial,
  20.  * amateur radio use only, as long as credit is given to the author.
  21.  *
  22.  * v1.0 920325
  23.  */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <time.h>
  28.   
  29. static char Copyright[] = "(C) '92 Johan. K. Reinalda, WG7J";
  30.   
  31. #define LEN 80
  32. #define HISTFILE "history"
  33. #define BACKUP ".bak"
  34. #define NULLCHAR (char *) 0
  35.   
  36. char hfile[LEN] = "/spool/";
  37. int days = 30;
  38. int expired = 0;
  39.   
  40. void
  41. main(argc,argv)
  42. int argc;
  43. char *argv[];
  44. {
  45.     register int i;
  46.     char *cp;
  47.     FILE *old, *new;
  48.     time_t now;
  49.     time_t age;
  50.     time_t bidtime;
  51.     char oldfile[LEN];
  52.     char buf[LEN];
  53.   
  54.     /* first get options, if any */
  55.     if(argc > 1) {
  56.         for(i=1;i<argc;i++) {
  57.             if(!strncmp(argv[i],"-d",2) && strlen(argv[i]) < LEN+1) {
  58.                 strcpy(hfile,&argv[i][2]);
  59.                 cp = &hfile[strlen(hfile)-1];
  60.                 if(*cp != '/')
  61.                     strcat(hfile,"/");
  62.             } else {
  63.                 if((days = atoi(argv[i])) == 0){
  64.                     printf("Invalid option: %s\n",argv[i]);
  65.                     return;
  66.                 }
  67.             }
  68.         }
  69.     }
  70.     strcat(hfile,HISTFILE);
  71.     strcpy(oldfile,hfile);
  72.     strcat(oldfile,BACKUP);
  73.   
  74.     /* delete a previous backup and rename current history file */
  75.     unlink(oldfile);
  76.     if(rename(hfile,oldfile) == -1) {
  77.         puts("Can't rename history file");
  78.         return;
  79.     }
  80.   
  81.     /* open backup for reading, create new history file */
  82.     if((old = fopen(oldfile,"rt")) == NULL) {
  83.         puts("Error opening history.bak");
  84.         return;
  85.     }
  86.     if((new = fopen(hfile,"wt")) == NULL) {
  87.         puts("Error creating history file");
  88.         return;
  89.     }
  90.   
  91.     now = time(&now);
  92.     age = (time_t)(days*86400L);
  93.   
  94.     while(fgets(buf,LEN,old) != NULL) {
  95.         if((cp=strchr(buf,'\n')) != NULLCHAR)
  96.             *cp = '\0';
  97.         if((cp=strchr(buf,' ')) != NULLCHAR) {
  98.             /*found one with timestamp*/
  99.             *cp = '\0';
  100.             cp++;   /* now points to timestamp */
  101.             if((bidtime = atol(cp)) == 0L)
  102.                 /*something wrong, re-stamp */
  103.                 fprintf(new,"%s %ld\n",buf,now);
  104.             else {
  105.                 /* Has this one expired yet ? */
  106.                 if(now - bidtime < age)
  107.                     fprintf(new,"%s %ld\n",buf,bidtime);
  108.                 else
  109.                     expired++;
  110.             }
  111.         } else {
  112.             /* This is an old one without time stamp,
  113.              * add to the new file with current time as timestamp
  114.              */
  115.             fprintf(new,"%s %ld\n",buf,now);
  116.         }
  117.     }
  118.   
  119.     fclose(old);
  120.     fclose(new);
  121.   
  122.     printf("%d bid's expired\n",expired);
  123.     return;
  124. }
  125.   
  126.